package org.sith.taskrunner.gwt.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.*;
import org.sith.taskrunner.gwt.client.presenter.Presenter;


public class DataLoadWidget extends PopupPanel implements IDataLoadView {

    private Presenter presenter;

    interface LoadDataWidgetUiBinder extends UiBinder<Widget, DataLoadWidget> {
    }

    private static LoadDataWidgetUiBinder ourUiBinder = GWT.create(LoadDataWidgetUiBinder.class);
    @UiField
    Label loadDataLabel;
    @UiField
    VerticalPanel mainPanel;
    @UiField
    Widget progressBarPanel;
    @UiField
    Button closeButton;

    public DataLoadWidget() {

//        initWidget(ourUiBinder.createAndBindUi(this));
        add(ourUiBinder.createAndBindUi(this));
        setAnimationEnabled(true);
        setGlassEnabled(true);
        setSize("150px", "200px");
        this.setPopupPosition((int) (Window.getClientWidth() * 0.45), (int) (Window.getClientHeight() * 0.45));

    }


    @Override
    public void setPresenter(Presenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void addLogLine(String message, boolean successfully) {

        if (successfully) {
            getLogLabel(message, mainPanel, "greenLabel");
        } else {
            getLogLabel(message, mainPanel, "redLabel");

        }
    }

    @Override
    public void showCloseButton() {
        closeButton.setVisible(true);
    }

    @Override
    public void close() {
        this.hide(true);

    }


    private void getLogLabel(String message, VerticalPanel panel, String labelStyleName) {
        Label label = new Label(message);
        label.setStyleName(labelStyleName);
        panel.add(label);
    }


    @UiHandler("closeButton")
    public void handleClick(ClickEvent event) {
        this.close();
    }
}